package com.zpark.cloud.alibaba.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @PackageName: com.zpark.cloud.alibaba.controller
 * @ClassName: ConsumerController
 * @Description:
 * @author: RZS
 * @date: 2023/6/15  9:11
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Value("${serverName}")
    private String serverName;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/demo/{id}")
    public String demo(@PathVariable("id") Integer id){
        return restTemplate.getForObject(serverName + "/provider/test/" + id, String.class);
    }

    @GetMapping("/choose")
    public Object chooseUrl() {
        ServiceInstance instance = loadBalancerClient.choose("SpringCloudAlibaba-Provider");
        return instance;
    }
}
